package com.summer.framework.core.tenant.aspect;

import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import com.summer.framework.core.tenant.annotation.IgnoreTenant;
import com.summer.framework.core.tenant.context.TenantContext;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.stereotype.Component;

/**
 * Description: 多租户忽略注解的切面实现
 * @auther Ice Yang
 * Date 2025/7/4 11:40
 */
@Aspect
@Component
@ConditionalOnBean(TenantLineHandler.class)
public class TenantIgnoreAspect {

    @Around("@annotation(ignoreTenant) || @within(ignoreTenant)")
    public Object aroundMethod(ProceedingJoinPoint joinPoint, IgnoreTenant ignoreTenant) throws Throwable {
        // 设置忽略租户上下文
        TenantContext.setTenantFlag(true);
        try {
            // 执行目标方法
            return joinPoint.proceed();
        } finally {
            // 清理上下文
            TenantContext.clearFlag();
        }
    }
}
